<script setup lang="ts">
import router from '@/router'
// 导入图片资源
import type { ProjectCardProps } from '../types/ProjectCardProps'
import { onMounted, ref, watch } from 'vue';
const props = defineProps<{ app: ProjectCardProps, }>()

const app = ref(props.app)
const likes = ref(0);
const collected = ref(false);
const liked = ref(false);
const views = ref(0);
// 监听 app 的变化
watch(() => props.app, (newApp) => {
  // console.log('New app data received:', newApp);
  app.value = newApp; // 更新 app 数据
});

// 获取应用数据
onMounted(() => {
  // console.log('Initial App Data:', app.value);
  likes.value = Number(app.value.likes || 0);
  views.value = Number(app.value.views || 0);
  collected.value = false;
  liked.value = false;
});

const handleMoreInfo = () => {
  // console.log('Navigating to project details with projectId:', props.app.projectId)
  if (props.app.projectId)
    router.push({ name: 'projectDetails', params: { projectId: props.app.projectId } })
}
</script>

<template>
  <div style="background-color: white; border-radius: 0.5rem; padding: 0.5rem;">
    <div style="background-color: white; display: flex; width: 100%;">
      <img :src="app.image" :alt="app.title" style="max-width: 40%; flex-shrink: 0; object-fit: cover;">
      <div style="display: flex; flex-direction: column;flex-grow: 1; max-width: 60%;">
        <h2 style="word-wrap: break-word; padding: 0.5rem; font-weight: bold; margin-bottom: 0%; padding-top: 0%;">
          {{ app.title }}
        </h2>

        <div style="padding: 0.5rem; padding-top: 0%;">
          <div
            v-for="(tag, index) in app.tags"
            :key="index"
            style="display: inline-block;
            border-color: grey;
            border-style: solid;
            border-width: 0.1rem;
            border-radius: 0.2rem;
            background-color: white;
            padding: 0.2rem;
            margin-right: 0.2rem;
            margin-bottom: 0.2rem"
          >
            {{ tag }}
          </div>
        </div>

        <span v-if="app.description === ''" />
        <p
          v-else
          style="padding: 0.5rem;
            padding-top: 0%;
            line-height: 2rem;
            font-size: 1rem;
            margin-bottom: 0%;
            display: -webkit-box;
            line-clamp: 2;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;"
        >
          {{ app.description }}
        </p>
      </div>
    </div>

    <div style="display: flex; justify-content: right;">
      <button class="detail-button" @click="handleMoreInfo">
        查看详情
      </button>
    </div>
  </div>
</template>

<style scoped>
.detail-button {
    background-color: #e6f0ff;
    color: #2669f3;
    border: none;
    font-size: 1rem;
    border-radius: 0.2rem;
    padding: 0.2rem 0.5rem 0.2rem 0.5rem;
    cursor: pointer;
}
</style>
